﻿using SSC.Models;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Threading;

namespace SSC.ViewModels
{
    public class SlotViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private ObservableCollection<SlotModel> _slots;
        private DispatcherTimer _dispatcherTimer;
        public ObservableCollection<SlotModel> Slots
        {
            get => _slots;
            set
            {
                if (_slots != value)
                {
                    _slots = value;
                    OnPropertyChanged(nameof(Slots));
                }
            }
        }

        public SlotViewModel()
        {
            InitializeSlots();
            _dispatcherTimer = new DispatcherTimer
            {
                Interval = TimeSpan.FromSeconds(1)  // 设置为每秒触发一次
            };
            // 设置定时器的回调方法
            _dispatcherTimer.Tick += DispatcherTimer_Tick;
            // 启动定时器
            _dispatcherTimer.Start();
        }

        private readonly Dictionary<int, string> _slotNameMap = new Dictionary<int, string>
        {
            { 1, "上下板" },
            { 2, "水洗" },
            { 3, "热水洗" },
            { 4, "金回收2" },
            { 5, "金回收1" },
            { 6, "镀硬金"},
            { 7, "镀金" },
            { 8, "镀锡" },
            { 9, "镀银" },
            { 10, "镀镍" },
            { 11, "镀铜" },
            { 12, "镀铅" },
            { 13, "镀锡铅" },
            { 14, "镀锡铅" },
            { 15, "镀锡铅" },
            { 16, "镀锡铅" },
            { 17, "镀锡铅" },
            { 18, "镀锡铅" },
            { 19, "镀锡铅" },
            { 20, "镀锡铅" },
            { 21, "镀锡铅" },
            { 22, "镀锡铅" },
            { 23, "镀锡铅" },
            { 24, "镀锡铅" },
            { 25, "镀锡铅" },
            { 26, "镀锡铅" },
            { 27, "镀锡铅" },
            { 28, "清洗槽1" },
            { 29, "清洗槽2" },
            { 30,"终检槽" }
        };
        //初始化槽体
        private void InitializeSlots()
        {
            Slots = new ObservableCollection<SlotModel>();
            for (int i = 1; i <= 30; i++)
            {
                string slotName = _slotNameMap.ContainsKey(i) ? _slotNameMap[i] : $"槽{i:D2}";
                Slots.Add(new SlotModel(i, slotName));
            }
        }
        // DispatcherTimer 的 Tick 事件回调方法
        private void DispatcherTimer_Tick(object sender, EventArgs e)
        {
            UpdateCranePosition();
        }
        private void UpdateCranePosition()
        {
            double APosition = ReadFromPlcA();
            double BPosition = ReadFromPlcB();
            CraneAPosition = APosition;
            CraneBPosition = BPosition; 
        }

        // 模拟从PLC读取天车位置的方法
        private int currentSlotA = 1;
        private double ReadFromPlcA()
        {
            double position = 70 * (currentSlotA - 1) + 20;
            currentSlotA++;
            if (currentSlotA > 10)
                currentSlotA = 1;
            return position;
        }
        private int currentSlotB = 10;
        private double ReadFromPlcB()
        {
            double position = 70 * (currentSlotB - 1) + 20;
            currentSlotB++;
            if (currentSlotB > 21)
                currentSlotB = 10;
            return position;
        }


        protected virtual void OnPropertyChanged(string propertyName)
            => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

        public void UpdateSlotData(int slotNumber, string? pCode, string? partName, double? setCurrent, double? actualCurrent, double? setTemperature, double? actualTemperature, int? setTime, int? currentTime)
        {
            var slot = Slots.FirstOrDefault(s => s.SlotNumber == slotNumber); // 查找指定槽号的槽位
            if (slot != null)
            {
                // 更新槽位的各个字段
                slot.PCode = pCode;
                slot.PartName = partName;
                slot.SetCurrent = setCurrent;
                slot.ActualCurrent = actualCurrent;
                slot.SetTemperature = setTemperature;
                slot.ActualTemperature = actualTemperature;
                slot.SetTime = setTime;
                slot.CurrentTime = currentTime;

                // 通知UI更新槽位数据
                // 由于 `SlotModel` 的每个属性已经实现了 `INotifyPropertyChanged`，所以不需要手动触发
            }
        }

        #region 设置通知属性
        private double? _setCurrent;
        public double? SetCurrent
        {
            get => _setCurrent;
            set
            {
                if (_setCurrent != value)
                {
                    _setCurrent = value;
                    OnPropertyChanged(nameof(SetCurrent));
                }
            }
        }

        private double? _actualCurrent;
        public double? ActualCurrent
        {
            get => _actualCurrent;
            set
            {
                if (_actualCurrent != value)
                {
                    _actualCurrent = value;
                    OnPropertyChanged(nameof(ActualCurrent));
                }
            }
        }

        private double? _setTemperature;
        public double? SetTemperature
        {
            get => _setTemperature;
            set
            {
                if (_setTemperature != value)
                {
                    _setTemperature = value;
                    OnPropertyChanged(nameof(SetTemperature));
                }
            }
        }

        private double? _actualTemperature;
        public double? ActualTemperature
        {
            get => _actualTemperature;
            set
            {
                if (_actualTemperature != value)
                {
                    _actualTemperature = value;
                    OnPropertyChanged(nameof(ActualTemperature));
                }
            }
        }

        private int? _setTime;
        public int? SetTime
        {
            get => _setTime;
            set
            {
                if (_setTime != value)
                {
                    _setTime = value;
                    OnPropertyChanged(nameof(SetTime));
                }
            }
        }

        private int? _currentTime;
        public int? CurrentTime
        {
            get => _currentTime;
            set
            {
                if (_currentTime != value)
                {
                    _currentTime = value;
                    OnPropertyChanged(nameof(CurrentTime));
                }
            }
        }
        #endregion

        #region 天车位置
        private double craneAPosition;
        public double CraneAPosition
        {
            get { return craneAPosition; }
            set
            {
                if (craneAPosition != value)
                {
                    craneAPosition = value;
                    OnPropertyChanged(nameof(CraneAPosition));
                }
            }
        }
        private double _craneBPosition;
        public double CraneBPosition
        {
            get => _craneBPosition;
            set
            {
                if (_craneBPosition != value)
                {
                    _craneBPosition = value;
                    OnPropertyChanged(nameof(CraneBPosition));
                }
            }
        }

        #endregion
    }

}
